package com.qywx.controller;

import com.qywx.entity.DepartmentEntity;
import com.qywx.service.DepartmentService;
import com.qywx.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName DepartmentController
 * @Description: TODO
 * @Author wmj
 * @Date 2020/3/2
 * @Version V1.0
 **/
@Api(value = "部门相关接口", tags = {"部门相关接口"})
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * @MethodName: save
     * @Description: 新增部门
     * @Param: [department]
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/9
    **/
    @PostMapping("/save")
    @ApiOperation(value = "新增部门", notes = "name:部门名称，parentid:父部门id")
    public R save(@RequestBody DepartmentEntity department){
        return departmentService.save(department);
    }

    /**
     * @MethodName: findByName
     * @Description: 根据部门名称搜索部门
     * @Param: [name]
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/9
    **/
    @GetMapping("/find/name")
    @ApiOperation(value = "根据部门名称查询部门", notes = "name：名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", required = true, value = "名称", paramType = "query", dataType = "String")
    })
    public R findByName(@RequestParam("name") String name){
        List<DepartmentEntity> departmentEntityList = null;
        if(name ==null){
            departmentEntityList = departmentService.getDepartmentList();
        }else {
            departmentEntityList = departmentService.findByName(name);
        }
        R r = new R();
        r.put("data",departmentEntityList);
        return r;
    }

    /**
     * @MethodName: updateDepartment
     * @Description: 更新部门
     * @Param: [departmentEntity]
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/9
    **/
    @PostMapping("/update")
    @ApiOperation(value = "更新部门名称", notes = "id:部门id")
    /*@ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "部门id", paramType = "query", dataType = "String")
    })*/
    public R updateDepartment(@RequestBody DepartmentEntity departmentEntity){
        return departmentService.updateDepartment(departmentEntity);
    }

    /**
     * @MethodName: delDepartmentById
     * @Description: 根据id删除部门
     * @Param: [id]
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/9
    **/
    @GetMapping("/delete")
    @ApiOperation(value = "根据部门id删除部门", notes = "部门id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "部门id", paramType = "query", dataType = "Long")
    })
    public R delDepartmentById(@RequestParam("id") Long id){
        return departmentService.delDepartmentById(id);
    }
    
    /** 
     * @MethodName: getDepartmentList
     * @Description: 获取部门列表
     * @Param: []
     * @Return: com.qywx.utils.R
     * @Author: wmj
     * @Date: 2020/3/12
    **/
    @GetMapping("/find")
    @ApiOperation(value = "获取所有部门", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depid", required = true, value = "部门id", paramType = "query", dataType = "int")
    })
    public R getDepartmentList(@RequestParam("depid")int depid){
        if(0!=depid){
            return departmentService.getDepartBydepid(depid);
        }else{
            List<DepartmentEntity> departmentEntity = departmentService.getDepartmentList();
            R r = new R();
            r.put(  "data",departmentEntity);
            return r;
        }

    }

   /* @GetMapping("/find/depid")
    @ApiOperation(value = "根据部门id查询部门", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depid", required = true, value = "部门id", paramType = "query", dataType = "Long")
    })
    public R getDepartBydepid(@RequestParam("depid") Long depid){
        return  departmentService.getDepartBydepid(depid);
    }*/

    @ApiOperation(value = "异步检测", notes = "")
    @GetMapping("/asynchronous/detection")
    public R asynchronousDetection(){
        return departmentService.asynchronousDetection();
    }

    @GetMapping("/synchronize")
    @ApiOperation(value = "异步检测后同步", notes = "")
    public R synchronize(){
        return departmentService.synchronize();
    }

    @GetMapping("/find/children")
    @ApiOperation(value = "根据部门id查询一级子部门", notes = "部门id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "部门id", paramType = "query", dataType = "Long")
    })
    public R findChildren(@RequestParam("id") Long id){
        return departmentService.findChildren(id);
    }

    @GetMapping("/delete/user")
    @ApiOperation(value = "删除部门成员", notes = "部门id,成员id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", required = true, value = "用户id，多个用户以逗号隔开", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "departmentid", required = true, value = "部门id", paramType = "query", dataType = "Long")
    })
    public R delUser(@RequestParam("userid") String userid,@RequestParam("departmentid") Long departmentid){
        return departmentService.delUser(userid,departmentid);
    }

    @GetMapping("/save/user")
    @ApiOperation(value = "增加部门成员", notes = "部门id,成员id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", required = true, value = "用户id，多个用户以逗号隔开", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "departmentid", required = true, value = "部门id", paramType = "query", dataType = "Long")
    })
    public R saveDepartUser(@RequestParam("userid") String userid,@RequestParam("departmentid") Long departmentid){
        return departmentService.saveDepartUser(userid,departmentid);
    }
}
